<template>
	<view class="container">
		<image src="/static/logo.png" class="logo"></image>
		<u-action-sheet :show="isShow" @close="isShow = false" title="隐私权限声明" :round="10" class="statement"
			:closeOnClickOverlay="false">
			<u-parse :content="statementData"></u-parse>
			<view class="btns">
				<navigator class="btn-exit" open-type="exit" target="miniProgram">不同意并退出</navigator>
				<button class="btn" @click="agreeState()">同意并继续</button>
			</view>
		</u-action-sheet>
		<view class="form-item">
			<button class="form-btn" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber"> 微信一键登录</button>
		</view>
		<!-- <view class="confirm">
			<checkbox-group>
				<checkbox color="red" :checked="agree" @click="()=>agree=!agree">我已阅读并同意
					<text @click.stop="showUserStatement()" class="font">{{getValue("用户协议")}}</text>和
					<text @click.stop="showPrivacyPolicy()" class="font">{{getValue("隐私协议")}}</text>
				</checkbox>
			</checkbox-group>
		</view> -->
		<!-- <u-modal :title="title" :show="showModal" :zoom="false" @confirm="confirm">
			<u-parse :content="statementData"></u-parse>
		</u-modal> -->
	</view>
</template>

<script>
	import store from '@/store/store.js'
	import _constant from "@/common/constant.js"
	export default {
		data() {
			return {
				isShow: false,
				statementData: "",
				checkout: false,
				showModal: false,
				title: '',
				agree: false,
				// userInfoTemp:{}
			};
		},
		onLoad() {
			// 初始化声明协议
			this.setStatementData(_constant.privacyTreatyId);
			// this.getCode();
			this.init()
		},
		onReady() {
			this.isShow = true
		},
		methods: {
			async init() {
				await this.getCode()
				await this.pathCode()
			},
			//获取登录临时code
			getCode() {
				uni.login({
					onlyAuthorize: true,
					success(e) {
						console.log('获取的code：' + e.code);
						store.dispatch('user/setCODE', e.code)
						// this.code = e.code
					}
				})
			},
			//通过临时code兑换openID
			pathCode() {
				const code = store.getters['user/code']
				return this.getOpenId(code)
			},
			async getOpenId(code) {
				const {
					data: res
				} = await uni.$http.get(uni.$http.baseURL + '/user/getOpenId', {
					code: code
				})
				if (res.status === 0) {
					store.dispatch('user/setOpenID', res.body)
					return res.body
				} else {
					uni.$u.sleep(1500).then(() => {
						this.init()
					})

				}
			},
			//获取手机号码
			async decryptPhoneNumber(e) {
				// if (!this.agree)
				// 	return
				const {
					data: res
				} = await uni.$http.get(uni.$http.baseURL + '/user/getPhoneByCode', {
					code: e.detail.code
				})
				if (res.status == 0) {
					console.log(res.body)
					uni.setStorageSync('phone', res.body)
					this.tryLogin()
				} else {
					uni.$showMsg(res.message)
				}
			},

			// 	getValue(value) {
			// 		return "<<" + value + ">>"
			// 	},
			// loginClick() {
			// 	// if (this.agree) {
			// 	this.getUserInfo()
			// 	// } else {
			// 	// 	uni.$showMsg("请勾选同意协议")
			// 	// }
			// },
			//尝试登录获取状态
			async tryLogin() {
				const {
					data: res
				} = await uni.$http.get(uni.$http.baseURL + '/developer/getDeveloperInfoByWxId', {
					wxId: uni.getStorageSync('openId')
				})
				if (res.status == 0) {
					//未注册或者未通过审核时跳转至提交信息页面
					if (res.body == null || res.body.status != 2) {
						uni.navigateTo({
							url: '/pages/login_form/login_form'
						})
					} else {
						//自动登录
						this.login(res.body)
					}
					// let les = {
					// 	// "userName": "测试",
					// 	"phone": "15001988966",
					// 	"wxId": "ojzfk4pS6IIQBKYR2SBEJzq0BZpY",
					// 	"password": "15001988966",
					// 	"roleId": "3",
					// }
					// this.login(les)
				} else {
					uni.$showMsg(res.message)
				}
			},
			async login(_para) {
				let _push_form = {}
				_push_form.wxId = _para.wxId
				_push_form.userType = '3'
				_push_form.phone = _para.phone
				_push_form.password = _para.password
				const {
					data: res
				} = await uni.$http.post(uni.$http.baseURL + '/developer/login', _push_form)
				if (res.status == 0) {
					uni.setStorageSync('authCode', res.body)
					uni.switchTab({
						url: "/pages/home/home"
					})
				} else {
					uni.$showMsg(res.message)
				}
			},
			// showUserStatement() {
			// 	this.setStatementData('123123123124');
			// 	this.title = '爱芯房用户协议'
			// 	this.showModal = true
			// },
			// showPrivacyPolicy() {
			// 	this.setStatementData('abhpfoxtvvpaweotxfbxhearnrykscuyfusbrlhebgvyfykmukqqcrsnrcozrbnu');
			// 	this.title = '爱芯房隐私权限'
			// 	this.showModal = true
			// },
			// confirm() {
			// 	this.showModal = false
			// 	this.agree = true
			// },
			//获取用户信息
			getUserInfo() {
				uni.getUserProfile({
					desc: "获取用户信息",
					complete: (res) => {
						if (res.errMsg === "getUserProfile:fail auth deny") return uni.showToast({
							title: "授权失败!",
							duration: 1500,
							icon: "error"
						})
						else {
							store.dispatch('user/setUserName', res.userInfo.nickName)
						}
						console.log(res.userInfo)
					}
				})
			},
			agreeState() {
				this.close()
				// 点击确认后，获取用户信息
				// this.getUserInfo()
			},
			//初始化声明协议
			async setStatementData(id) {
				const {
					data: res
				} = await uni.$http.get(uni.$http.baseURL + '/user/userAgreement', {
					agreeId: id
				})
				this.statementData = res.body.text
			},
			goto(url) {
				uni.navigateTo({
					url: url
				})
			},
			// 获取openID，验证账号是否存在
			// async tranferOpenId(code) {
			// 	const {
			// 		data: res
			// 	} = await uni.$http.get(uni.$http.baseURL + '/user/getOpenId', {
			// 		code: code
			// 	})
			// 	store.dispatch('user/setOpenID', res.body)
			// 	const {
			// 		data: res2
			// 	} = await uni.$http.get(uni.$http.baseURL + '/user/isExist', {
			// 		wxId: res.body
			// 	})
			// },
			close() {
				this.isShow = false
			}
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		flex-direction: column;

		.logo {
			height: 211rpx;
			width: 175rpx;
			margin-top: 302rpx;
			margin-left: 287rpx;
		}
	}

	.statement {
		.btns {
			display: flex;
			flex-direction: row;
			// justify-content: space-around;
			padding: 80rpx 45rpx 60rpx 45rpx;

			.btn,
			.btn-exit {
				width: 300rpx;
				height: 80rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 2rpx 10rpx 0rpx rgba(0, 0, 0, 0.18);
				border-radius: 10rpx;
			}

			.btn-exit {
				line-height: 80rpx;
				color: #24C53E;
			}

			.btn {
				background: #24C53E;
				color: #FFFFFF;
			}
		}
	}

	.confirm {
		position: absolute;
		bottom: 114rpx;
		left: 78rpx;
		font-size: 24rpx;
		font-weight: bold;
		color: #333333;

		.font {
			color: #E62029;
		}
	}

	.form-item {
		margin-top: 44rpx;
		padding-left: 50rpx;
		padding-right: 50rpx;
	}

	.form-btn {
		color: #FFFFFF;
		height: 88rpx;
		width: 100%;
		margin-top: 105rpx;
		border: 1rpx solid #E62029;
		border-radius: 44rpx;
		background: linear-gradient(90deg, #E62129 0%, #BA000A 100%);
		box-shadow: 0rpx 5rpx 15rpx 0rpx rgba(0, 0, 0, 0.05);
	}
</style>
